﻿#include <cmath>
#include <QDebug>
#include <QFontMetrics>
#include <QMessageBox>
#include "element.h"
#include "MeasureAlgorithm.h"

#define M_PI 3.14159265358979323846
namespace Qly
{
DrawArc::DrawArc(void)
{
    m_drawType = ARC;
    m_state = DS_IDLE;
}

DrawArc::~DrawArc(void)
{

}

int DrawArc::mouseLButtonPress(QPointF point)
{
    if( m_state == DS_IDLE )
    {
        m_points[0] = point;
        m_state = DS_STATE1;
        return 1;
    }
    if(m_state == DS_STATE1)
    {
        m_points[1] = point;
        m_state = DS_STATE2;
        return 2;
    }
    if(m_state == DS_STATE2)
    {
        m_points[2] = point;
        compute();
        return 3;
    }
    return 0;
}

void DrawArc::compute()
{
    if( m_circleMeasure.buildBy3Points(QPointF(m_points[0]), QPointF(m_points[1]), QPointF(m_points[2])) )
    {
        m_radius = m_circleMeasure.m_radius;
        m_centre = m_circleMeasure.m_centre.toPoint();
        m_state = DS_FINISH;    
        updateText();
        m_textBlock.moveTo(m_centre);
    }
}

void DrawArc::draw( QPainter &painter )
{
    if( m_state != Element::DS_IDLE )
    {
        painter.save();
        int x, y, width, height;
        x = m_centre.x() - m_radius;
        y = m_centre.y() - m_radius;
        width = 2 * m_radius;
        height = 2 * m_radius;


        double theta1 = m_circleMeasure.m_theta[0] * 180 / M_PI * 16;
        double det_theta = m_circleMeasure.m_det_theta[1] * 180 / M_PI * 16;

        m_linePen.setStyle(Qt::DashLine);
        painter.setPen(m_linePen);
        painter.drawLine(m_circleMeasure.m_centre, m_circleMeasure.m_points[0]);
        painter.drawLine(m_circleMeasure.m_centre, m_circleMeasure.m_points[2]);
        m_linePen.setStyle(Qt::SolidLine);
        painter.setPen(m_linePen);

        if(m_circleMeasure.m_rotateDir > 0)
        {
            painter.drawArc ( x, y, width, height, theta1, det_theta);
        }
        else
        {
            painter.drawArc ( x, y, width, height, theta1, det_theta);
        }

        if( m_state == Element::DS_STATE1 )
        {
            painter.drawPoint(m_points[0]);
        }
        else if( m_state == Element::DS_STATE2 )
        {
            QPointF points[2];
            points[0] = m_points[0];
            points[1] = m_points[1];
            painter.drawPoints(points, 2);
        }
        else if( m_state == Element::DS_FINISH )
        {
            QPointF points[3];
            points[0] = m_points[0];
            points[1] = m_points[1];
            points[2] = m_points[2];
            painter.drawPoints(points, 3);

            painter.drawLine(m_centre.x() - 5, m_centre.y(), m_centre.x() + 5, m_centre.y());
            painter.drawLine(m_centre.x(), m_centre.y() - 5, m_centre.x(), m_centre.y() + 5);

            //painter.setFont(m_textFont);
            m_textBlock.setFont(m_textFont);
            m_textBlock.draw(painter);
        }
        painter.restore();
    }

}
void DrawArc::updateText(void)
{
    if(m_radius * scale() >= 500.0)
    {
        m_text = tr(u8"D = %1 mm,θ = %2° ")
                .arg(2 * m_radius * scale() / 1000.0, 2, 'f', 2)
                .arg(m_circleMeasure.m_det_theta[1] * 180 / 3.14159);
    }
    else
    {
        m_text = QString(u8"R = %1 μm,θ = %2° ")
                .arg(m_radius * scale(), 2, 'f', 2)
                .arg(m_circleMeasure.m_det_theta[1] * 180 / 3.14159);
    }
    m_textBlock.setText(m_text);
    //qDebug() << m_text;
}

void DrawArc::computeBox(void)
{

}

void DrawArc::build(QPoint p1, QPoint p2, QPoint p3)
{
    m_points[0] = p1;
    m_points[1] = p2;
    m_points[2] = p3;
    compute();
}


} //namespace Qly
